package Java.com.hcj.realization;

import Java.com.hcj.dao.TableDao;
import Java.com.hcj.entity.Table;
import Java.com.hcj.realization.utils.DataType;
import Java.com.hcj.realization.utils.Folder;
import Java.com.hcj.realization.utils.Hump;
import Java.com.hcj.service.TableService;
import Java.com.hcj.utils.Entity;
import Java.com.hcj.utils.Xml;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by an on 2016/9/23.
 */
public class TableRealization {

    /*
    可以把service的重写方法静态化
    直接调用该静态查询方法
    下面这个只是为了测试接口方法的重写,与使用
     */
    //实现service层方法
    TableDao tableDao=new TableService();
    DataType dataType=new DataType();
    Hump hump=new Hump();
    Folder folder=new Folder();
    //数据库名/表名参数
    public  List<Table> data(String dataSheet){
        Table tables=new Table();
        tables.setDataSheet(dataSheet);
        List<Table> table=tableDao.findByName(tables);
        return table;
    }

    public String create(String tableName,String packageName,String className,String author){
        TableRealization tableContrller=new TableRealization();
        List<Table> list=tableContrller.data(tableName);
        if (list.size()==0){
            return "2";
        }
        List<Object> list1=new ArrayList<Object>();
        List<Object> list2=new ArrayList<Object>();
        for (int i=0;i<list.size();i++){
            list1.add(new Entity(hump.toCamelCase(list.get(i).getColumnName()),dataType.getNewDataType(list.get(i).getDataType())));
        }
        for (int i=0;i<list.size();i++){
            list2.add(new Xml(list.get(i).getColumnName(),list.get(i).getDataType().toUpperCase(),dataType.getNewDataType(list.get(i).getDataType()),hump.toCamelCase(list.get(i).getColumnName())));
        }
        Map root = new HashMap();//实体
        root.put("packageName", packageName);
        root.put("className",hump.toCapitalizeCamelCase(className));
        root.put("className1",hump.toCamelCase(className));
        root.put("entity", list1);
        root.put("author",author);

        Map xml = new HashMap();//实体
        xml.put("packageName", packageName);
        xml.put("className",hump.toCapitalizeCamelCase(className));
        xml.put("className1",hump.toCamelCase(className));
        xml.put("xml", list2);
        xml.put("table",tableName);
        xml.put("m","{");
        xml.put("n","}");
        xml.put("o","<!--");
        xml.put("p","-->");

        Configuration cfg =new Configuration(Configuration.VERSION_2_3_25);
        String path = TableRealization.class.getResource("/Java/").getPath()+"template";
        try {
            cfg.setDirectoryForTemplateLoading(new File(path));//模版路径
            Template template = cfg.getTemplate("/entity.ftl");//entity层
            Template template1 = cfg.getTemplate("/dao.ftl");//dao层
            Template template2 = cfg.getTemplate("/service.ftl");//service层
            Template template3 = cfg.getTemplate("/web.ftl");//web层
            Template template4 = cfg.getTemplate("/xml.ftl");//xml层
            StringWriter entity = new StringWriter();
            StringWriter dao=new StringWriter();
            StringWriter service=new StringWriter();
            StringWriter web=new StringWriter();
            StringWriter xmls=new StringWriter();
            template.process(root, entity);
            template1.process(root, dao);
            template2.process(root, service);
            template3.process(root, web);
            template4.process(xml, xmls);

            String  entityName=folder.folder(packageName+".entity");//生成文件夹实体层
            String daoName=folder.folder(packageName+".dao");//生成文件夹dao层
            String serviceName=folder.folder(packageName+".service");//生成文件夹service层
            String controllerName=folder.folder(packageName+".controller");//生成文件夹controller层
            String xmlName=folder.folder(packageName+".mapping");//生成文件夹mapping xml文件层
            //对应生成文件
            String entitys=hump.toCapitalizeCamelCase(className)+".java";
            String daos=hump.toCapitalizeCamelCase(className)+"Dao.java";
            String services=hump.toCapitalizeCamelCase(className)+"Service.java";
            String control=hump.toCapitalizeCamelCase(className)+"Controller.java";
            String xmlss=hump.toCapitalizeCamelCase(className)+"Mapper.xml";
            try {
                FileWriter fw = new FileWriter(entityName + "\\" + entitys,false);  // FileWriter 就是牛，如果文件名 的文件不存在，先创建再读写;存在的话直接追加写,关键字true表示追加
                fw.write(String.valueOf(entity));
                fw.close(); // 关闭写文件,每次仅仅写一行数据，因为一个读文件中仅仅一个唯一的od
                FileWriter fw1 = new FileWriter(daoName + "\\" + daos,true);
                fw1.write(String.valueOf(dao));
                fw1.close();
                FileWriter fw2 = new FileWriter(serviceName + "\\" + services,true);
                fw2.write(String.valueOf(service));
                fw2.close();
                FileWriter fw3 = new FileWriter(controllerName + "\\" + control,true);
                fw3.write(String.valueOf(web));
                fw3.close();
                FileWriter fw4 = new FileWriter(xmlName + "\\" + xmlss,true);
                fw4.write(String.valueOf(xmls));
                fw4.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return "status:"+"0";
        } catch (IOException e) {
            e.printStackTrace();
            return "status:"+"1";
        } catch (TemplateException e) {
            e.printStackTrace();
            return "status:"+"1";
        }
    }
}
